/*

 * Copyright (C) 2021 Huawei Device Co., Ltd.

 * Licensed under the Apache License, Version 2.0 (the "License");

 * you may not use this file except in compliance with the License.

 * You may obtain a copy of the License at

 *

 *     http://www.apache.org/licenses/LICENSE-2.0

 *

 * Unless required by applicable law or agreed to in writing, software

 * distributed under the License is distributed on an "AS IS" BASIS,

 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

 * See the License for the specific language governing permissions and

 * limitations under the License.

 */
package zaizai.com;

import com.ajguan.library.EasyRefreshLayout;
import com.ajguan.library.exception.ERVHRuntimeException;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.Component;
import ohos.app.Context;

import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;

/**
 * 单元测试
 */
public class ExampleOhosTest {
    /**
     * {@link EasyRefreshLayout}
     * 列表界面刷新加载更多控件
     */
    EasyRefreshLayout easyRefreshLayout;

    /**
     * 初始化测试数据
     */
    @Before
    public void setUp() {
        Context appContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        easyRefreshLayout = new EasyRefreshLayout(appContext);
    }

    /**
     * 测试{@link EasyRefreshLayout#setRefreshHeadView(Component)}
     */
    @Test
    public void setRefreshHeadView() {
        easyRefreshLayout.setRefreshHeadView(null);
        easyRefreshLayout.refreshComplete();
    }

    /**
     * 测试{@link EasyRefreshLayout#onEstimateSize(int, int)}
     */
    @Test
    public void onEstimateSize() {
        easyRefreshLayout.onEstimateSize(213123, 111);
    }

    /**
     * 测试{@link EasyRefreshLayout#setEnablePullToRefresh(boolean)}
     */
    @Test
    public void setEnablePullToRefresh() {
        easyRefreshLayout.setEnablePullToRefresh(false);
        easyRefreshLayout.autoRefresh();
        assertFalse(easyRefreshLayout.isRefreshing());
    }

    /**
     * 测试{@link EasyRefreshLayout#setLoadMoreView(Component)}
     * if set null throw {@link ERVHRuntimeException}
     */
    @Test(expected = ERVHRuntimeException.class)
    public void setLoadMoreView() {
        easyRefreshLayout.setLoadMoreView(null);
    }

    /**
     * 测试{@link EasyRefreshLayout#setAdvanceCount(int)}
     */
    @Test
    public void setAdvanceCount() {
        easyRefreshLayout.setAdvanceCount(0);
        assertEquals(easyRefreshLayout.getAdvanceCount(), 0);
    }

    /**
     * 测试{@link EasyRefreshLayout#autoRefresh(long)}
     */
    @Test(timeout = 1000)
    public void autoRefresh() {
        easyRefreshLayout.autoRefresh(1000);
    }

    /**
     * 测试{@link EasyRefreshLayout#setScrollToTopDuration(int)}
     */
    @Test
    public void setScrollToTopDuration() {
        easyRefreshLayout.setScrollToRefreshDuration(1000);
        assertEquals(easyRefreshLayout.getScrollToRefreshDuration(), 1000);
    }

    /**
     * 测试BundleName
     */
    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("com.ajguan", actualBundleName);
    }
}